import threading, time
class Hider(threading.Thread):
    def __init__(self, cond, name):
        super(Hider, self).__init__()
        self.cond = cond
        self.name = name

    def run(self):
        time.sleep(1)
        self.cond.acquire()
        print self.name + ':close my eye'
        self.cond.notify()
        self.cond.wait(10)
        print self.name + ':found you'
        self.cond.notify()
        self.cond.release()
        print self.name + ':i win'
class Seeker(threading.Thread):
    def __init__(self, cond, name):
        super(Seeker, self).__init__()
        self.cond = cond
        self.name = name
    def run(self):
        self.cond.acquire()
        self.cond.wait(10)
        print self.name + ':hidden'
        self.cond.notify()
        self.cond.wait(10)
        self.cond.release()   
        print self.name + ':be found'  
        



class User(threading.Thread):
    def __init__(self, cond, name):
        super(User, self).__init__()
        self.cond = cond
        self.name = name
        self.msgs = []

    def addMsg(self, msg):
        self.cond.acquire()
        self.msgs.append(msg)
        print '>>>%s' % self.msgs
        self.cond.notify()
        self.cond.release()

    def getMsg(self):
        self.cond.acquire()
        if self.msgs:
            return self.msgs.pop()
        else:
            self.cond.wait()
            self.cond.notify()
            return self.msgs.pop()

    def run(self):
        while(True):
            print '<<<%s' % self.getMsg()



cond = threading.Condition()
user = User(cond, 'USER1')
user.start()

while(True):
    import time
    import random
    time.sleep(random.randint(1, 3))
    user.addMsg('haha')
